package com.rc.saas.payment.unwxpay.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
* Zip包压缩和解压缩工具
* @author LGM
* @version 13.0.1 2013/09/28
* @version 15.0.1 2015/07/14
*/
public class ZipUtil {
    static final int BUFFER = 2048;
    


	//-----------------------------Version 17.0.1 Begin---------------------------//
	public byte[] Gzip(byte[] datas) throws Exception { 
        List<ZipItem>item = new ArrayList<ZipItem>();
        item.add(new ZipItem("", datas));
        return Gzip(item);
    }

    public byte[] unGzip(byte[] datas) throws Exception {
        ByteArrayInputStream inp = new ByteArrayInputStream(datas);
        GZIPInputStream zipInput = new GZIPInputStream(inp);
        byte[] buf = new byte[1024];  
        int num = -1;  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        while ((num = zipInput.read(buf, 0, buf.length)) != -1) {  
            baos.write(buf, 0, num);    
        }
        zipInput.close();
        return baos.toByteArray();
    }



    public static byte[] Gzip(List<ZipItem> zipItems) {  
        byte[] b = null;  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
        GZIPOutputStream zip;  
        try {  
            zip = new GZIPOutputStream(bos); 
            for (ZipItem item : zipItems) {            
                zip.write(item.zipBody);
            }
            zip.close();  
            b = bos.toByteArray();  
            bos.close();  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        } finally{
            try {
                bos.close();
            } catch (IOException e) {}
        }
        return b;  
    }  

    private static class ZipItem{

        private byte[] zipBody;

        ZipItem(String zipName,byte[]zipBody){
            this.zipBody = zipBody;
        }
    }
   //-----------------------------Version 17.0.1 End---------------------------//
    
    public static void main(String argv[]) {
    	//version 13.0.1
        // File f= new File("d:\\111\\");
        //String[] filenames = new String[]{"E:/mnpdata/ad/temp/1209014565259/郁香.jpg"};
        //zip(filenames,"c:/myfui.zip");
    	
    	//version 15.0.1
//    	String zipDirectory = "E:\\SVN\\Workspaces\\MyEclipse for Spring 10\\.metadata\\.me_tcat\\webapps\\meterial\\merchant\\5";
//    	String zipPath = "C:\\Users\\Administrator\\Desktop";
//    	File file = doZip(zipDirectory, zipPath);
//    	System.out.println("测试压缩");
    	
//    	String unZipfileName = "C:\\Users\\Administrator\\Desktop\\5.zip";
//    	String unZipPath = "F:\\";
//    	boolean result = unZip(unZipfileName, unZipPath);
//    	System.out.println("测试解压缩");
    }
}